﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;
using Styx.Common;

namespace CommunicationBuddy
{
    public class Settings
    {

        public bool whisperAlert;
        public bool answering;
        public string wordlist;
        public string Loglevel;
        public bool delay;
        public int delayV1;
        public int delayV2;

        public Settings()
        {

            answering = true;
            whisperAlert = true;
            Loglevel = "Normal";
            wordlist = "wordlist.xml";
            delay = false;
            delayV1 = 2000;
            delayV2 = 4000;
        }

    }

    class SXml
    {

        public static Settings settings = new Settings();

        public static bool Save(Settings settings_, bool silent)
        {

            try
            {

                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                TextWriter textWriter = new StreamWriter(Application.StartupPath + @"\Plugins\CommunicationBuddy\Settings.xml");
                serializer.Serialize(textWriter, settings_);
                textWriter.Close();
            }
            catch (Exception ee)
            {

                Logging.Write("[CommunicationBuddy] Error while saving :\n" + ee.Message);
                return false;
            }
            if (!silent)
                Logging.Write("[CommunicationBuddy] Saved settings.");

            return true;
        }

        public static bool Load(bool silent)
        {

            Settings temp;
            try
            {

                XmlSerializer deserializer = new XmlSerializer(typeof(Settings));
                TextReader textReader = new StreamReader(Application.StartupPath + @"\Plugins\CommunicationBuddy\Settings.xml");
                temp = (Settings)deserializer.Deserialize(textReader);
                textReader.Close();
            }
            catch (Exception ee)
            {

                Logging.Write("[CommunicationBuddy] ----WARNING----");
                Logging.Write("[CommunicationBuddy] Error while loading settings :\n" + ee.Message);
                settings = new Settings();
                return false;
            }
            if (!silent)
                Logging.Write("[CommunicationBuddy] Settings loaded, great success!");

            settings = temp;
            return true;

        }

    }
}
